package org.eclipse.wb.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test extends Shell {

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			Test shell = new Test(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public Test(Display display) {
		super(display, SWT.SHELL_TRIM);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(450, 300);
		setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		StyledText styledText = new StyledText(composite, SWT.BORDER);
		styledText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false,
				1, 1));
		styledText.setText("距离到达现场还有：");
		styledText.setBackground(SWTRes
				.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		styledText.setEnabled(false);
		styledText.setEditable(false);

		styledText.setStyleRange(getColorStyle(2, 4, SWTRes
				.getColor(SWT.COLOR_BLUE)));

		StyledText styledText_1 = new StyledText(composite, SWT.BORDER
				| SWT.SINGLE | SWT.CENTER);
		styledText_1.setText("你是我心内的一首歌");
		
		styledText_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				true, 1, 1));
		
		TipLabel label = new TipLabel(composite);
		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		label.setText("ddfdfee喱顺中");
	}

	private StyleRange getColorStyle(int startOffset, int length, Color color) {
		StyleRange styleRange = new StyleRange(startOffset, length, color, null);
		styleRange.fontStyle = SWT.BOLD;
		return styleRange;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}

class TipLabel extends StyledText {

	public TipLabel(Composite parent) {
		super(parent, SWT.SINGLE | SWT.CENTER);
		init();
	}

	private void init() {
		setAlignment(SWT.CENTER);
		setBackground(SWTRes.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		setEnabled(false);
	}
}